package org.example.DesignPatterns.Singleton;

/**
 * @description:
 * @author: farprt
 * @create: 2022-09-12 17:18
 **/
public class LazySun {

    //volatile防止指令重排
    private volatile static LazySun lazySun;

    private LazySun(){
        System.out.println(Thread.currentThread().getName()+"ok");
    }

    public static LazySun getInstance(){
        if(lazySun==null){
            synchronized (LazySun.class){
                if(lazySun==null){
                    lazySun=new LazySun();
                }
            }
        }
        return lazySun;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                LazySun.getInstance();
            }).start();
        }
    }
}